﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;

namespace SharePointListSecurity
{
    class UsersAndGroupsManager
    {
        private SPWeb spWeb;
        #region ctor
        public UsersAndGroupsManager(SPWeb spWeb)
        {
            this.spWeb = spWeb;
        }
        #endregion

        #region public Methods

        public bool IsSharepointUser(int id, out SPUser user)
        {
            try
            {
                user = spWeb.SiteUsers.GetByID(id);
                return true;
            }
            catch (Exception)
            {
                user = null;
                return false;
            }
        }
        public bool IsSharepointGroup(int? id, out SPGroup group)
        {
            try
            {
                group = spWeb.SiteGroups.GetByID(Convert.ToInt32(id));
                return true;
            }
            catch (Exception)
            {
                group = null;
                return false;
            }
        }
        public bool IsMember(SPGroup group, SPUser user)
        {
            try
            {
                group.Users.GetByID(user.ID);
                return true;
            }

            catch
            {
                return false;
            }


        }

        public bool IsADGroup(string name, out SPPrincipalInfo[] groupUsers, out SPPrincipalInfo userInfo)
        {
            bool reachedMax;
            groupUsers = null;
            userInfo = null;
            try
            {
                userInfo = SPUtility.ResolvePrincipal(spWeb, Convert.ToString(name),
                              SPPrincipalType.SecurityGroup, SPPrincipalSource.All, null, false);

                if (userInfo != null)
                {
                    groupUsers = SPUtility.GetPrincipalsInGroup(spWeb, name, 10000, out reachedMax);
                    return true;
                }

                return false;
            }

            catch
            {
                return false;
            }

        }

        public List<string> GetLoginNames(IList<string> UsersAndGroups, IList<int?> UsersAndGroupsIds)
        {
            int index = 0;
            List<string> loginNames = new List<string>();
            foreach (var item in UsersAndGroupsIds)
            {
                int id = Convert.ToInt32(item);

                SPUser user;
                if (IsSharepointUser(id, out user))
                {
                    loginNames.Add(user.LoginName);
                }
                else
                {
                    SPGroup group;
                    if (IsSharepointGroup(id, out group))
                    {

                        loginNames.Add(group.LoginName);
                    }
                    else
                    {
                        SPPrincipalInfo[] groupUsers;
                        SPPrincipalInfo userInfo;
                        string name = UsersAndGroups.ElementAt(index);
                        if (IsADGroup(name, out groupUsers, out userInfo))
                        {
                            loginNames.Add(userInfo.LoginName);
                        }
                    }
                }
                index++;
            }

            return loginNames;
        }

        #endregion
    }
}
